package com.shopping.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shopping.common.Result;
import com.shopping.entity.MemberLevelDiscount;
import com.shopping.service.MemberLevelDiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/level-discount")
public class AdminMemberLevelDiscountController {
    @Autowired
    private MemberLevelDiscountService memberLevelDiscountService;

    @GetMapping
    public Result<List<MemberLevelDiscount>> list() {
        return Result.success(memberLevelDiscountService.list());
    }

    @PostMapping
    public Result<MemberLevelDiscount> add(@RequestBody MemberLevelDiscount discount) {
        memberLevelDiscountService.save(discount);
        return Result.success(discount);
    }

    @PutMapping
    public Result<MemberLevelDiscount> update(@RequestBody MemberLevelDiscount discount) {
        memberLevelDiscountService.updateById(discount);
        return Result.success(discount);
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        memberLevelDiscountService.removeById(id);
        return Result.success();
    }

    @GetMapping("/by-level/{level}")
    public Result<MemberLevelDiscount> getByLevel(@PathVariable String level) {
        MemberLevelDiscount discount = memberLevelDiscountService.getOne(new QueryWrapper<MemberLevelDiscount>().eq("level", level));
        return Result.success(discount);
    }
}